//
//  TWLikeAnimation.m
//  特效动画
//
//  Created by 耐克了解了 on 31/5/17.
//  Copyright © 2017年 耐克了解了. All rights reserved.
//

#import "TWLikeAnimation.h"

@interface TWLikeAnimation ()

@property (nonatomic,strong) UIImageView *imageView;

@property (nonatomic,strong) UIColor *fillColor;

@property (nonatomic,strong) UIColor *strokeColor;

@end

@implementation TWLikeAnimation

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.fillColor = [UIColor colorWithRed:(arc4random_uniform(255))/255.0 green:(arc4random_uniform(255))/255.0 blue:(arc4random_uniform(255))/255.0 alpha:1];
        self.strokeColor = [UIColor clearColor];
        self.backgroundColor = [UIColor clearColor];
        self.layer.anchorPoint = CGPointMake(0.5, 1);
       // self.imageView = [[UIImageView alloc]initWithFrame:frame];
      //  [self addSubview:self.imageView];
        
    }
    return self;
}

- (void)animatePictureInView:(UIView *)view Image:(UIImage *)image
{
    self.imageView.image = image;
    NSTimeInterval totalAnimationDuration = 8;
    CGFloat heartSize = CGRectGetWidth(self.bounds); // 40
    CGFloat heartCenterX = self.center.x;
    CGFloat ViewHeight = CGRectGetHeight(view.bounds);
    
    
    self.transform = CGAffineTransformMakeScale(0, 0);
    self.alpha =0.0;
    
    [UIView animateWithDuration:0.5 delay:0.0 usingSpringWithDamping:0.6 initialSpringVelocity:0.8 options:UIViewAnimationOptionCurveEaseOut animations:^{
        
        self.transform =CGAffineTransformIdentity;
        self.alpha = 0.9;
        
    } completion:nil];
    
    NSInteger i = arc4random_uniform(2);
    
    NSInteger rotationDirection = 1 - (2 * i);
    NSInteger rotationFraction = arc4random_uniform(10);
    [UIView animateWithDuration:totalAnimationDuration animations:^{
        self.transform = CGAffineTransformMakeRotation(rotationDirection * M_PI / (16 + rotationFraction * 0.2));
    }];
    
    UIBezierPath *heartTravelPath = [UIBezierPath bezierPath];
    [heartTravelPath moveToPoint:self.center];
    
    //end Point
    CGPoint endPoint = CGPointMake(heartCenterX + (rotationDirection) * arc4random_uniform(2 * heartSize), ViewHeight / 6.0 +arc4random_uniform(ViewHeight / 4.0));
    
    NSInteger j = arc4random_uniform(2);
    NSInteger travelDirection = 1 - (2 * j);
    
    CGFloat xDelta = (heartSize / 2.0 + arc4random_uniform(2 * heartSize)) * travelDirection;
    CGFloat yDelta = MAX(endPoint.y, MAX(arc4random_uniform(8 * heartSize), heartSize));
    CGPoint controlPoint1 = CGPointMake(heartCenterX + xDelta, ViewHeight - yDelta);
    CGPoint controlPoint2 = CGPointMake(heartCenterX - 2 * xDelta, yDelta);
    
    [heartTravelPath addCurveToPoint:endPoint controlPoint1:controlPoint1 controlPoint2:controlPoint2];
    
    CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    keyAnimation.path = heartTravelPath.CGPath;
    keyAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    keyAnimation.duration = totalAnimationDuration + endPoint.y / ViewHeight;
    [self.layer addAnimation:keyAnimation forKey:@"positionOnPath"];
    
    [UIView animateWithDuration:totalAnimationDuration animations:^{
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}

- (void)drawRect:(CGRect)rect
{
    [self.strokeColor setStroke];
    
    [self.fillColor setFill];
    
    CGFloat drawingPadding = 4.0;
    
    CGFloat curveRadius = floor((CGRectGetWidth(rect) - 2 * drawingPadding) / 4.0);
    
    UIBezierPath *heartPath = [UIBezierPath bezierPath];
    
    CGPoint tipLocation = CGPointMake(floor(CGRectGetWidth(rect) / 2.0), CGRectGetHeight(rect) - drawingPadding);
    [heartPath moveToPoint:tipLocation];
    
    CGPoint topLeftCurveStart = CGPointMake(drawingPadding, floor(CGRectGetHeight(rect) / 2.4));
    
    [heartPath addQuadCurveToPoint:topLeftCurveStart controlPoint:CGPointMake(topLeftCurveStart.x, topLeftCurveStart.y + curveRadius)];
    
    [heartPath addArcWithCenter:CGPointMake(topLeftCurveStart.x + curveRadius, topLeftCurveStart.y) radius:curveRadius startAngle:M_PI endAngle:0 clockwise:YES];
    
    CGPoint topRightCurveStart = CGPointMake(topLeftCurveStart.x + 2 * curveRadius, topLeftCurveStart.y);
    
    [heartPath addArcWithCenter:CGPointMake(topRightCurveStart.x + curveRadius, topRightCurveStart.y) radius:curveRadius startAngle:M_PI endAngle:0 clockwise:YES];
    
    CGPoint topRightCurveEnd = CGPointMake(topLeftCurveStart.x + 4 * curveRadius, topRightCurveStart.y);
    [heartPath addQuadCurveToPoint:tipLocation controlPoint:CGPointMake(topRightCurveEnd.x, topRightCurveEnd.y + curveRadius)];
    
    [heartPath fill];
    heartPath.lineWidth = 1;
    heartPath.lineCapStyle = kCGLineCapRound;
    heartPath.lineJoinStyle = kCGLineCapRound;
    [heartPath stroke];
    
}





































@end
